const KoaRouter = require("koa-router");
const path = require('path');
const { sep } = path;
const glob = require("glob");
module.exports = (app) => {
  const routerPath = path.resolve(app.businessPath, `.${sep}router`);
  const router = new KoaRouter();
  const filelist = glob.sync(path.resolve(routerPath, `.${sep}**${sep}**.js`));
  filelist.forEach((file) => {
    require(path.resolve(file))(app);
  });
  router.get("*", async (ctx, next) => {
    ctx.status = 302;
    ctx.redirect(`${app?.options?.homePath ?? "/"}`);
  });
  // 路由注册到app上
  app.use(router.routes());
  app.use(router.allowedMethods());
};
